package controller;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.ArrayList;

import model.kwalificatiebeheer.Competentie;
import model.kwalificatiebeheer.CompetentieBestand;
import model.kwalificatiebeheer.Criterium;
import model.kwalificatiebeheer.Kerntaak;
import model.kwalificatiebeheer.Werkproces;

import view.AddCompetentieToWerkproces;
import view.AddObjectOptionPane;
import view.ComponentenTableModel;

public class AddCompetentieToWerkprocesActions implements MouseListener,
		ActionListener {

	AddCompetentieToWerkproces sender;
	
	public AddCompetentieToWerkprocesActions(AddCompetentieToWerkproces sender){
		this.sender = sender;
	}
	
	@Override
	public void actionPerformed(ActionEvent e) {
		if(e.getActionCommand().equals("btnSelect")){
			if(sender.getActiveCompetentie() != null){
				sender.initForm();
			}
		}else if(e.getActionCommand().equals("btnSave")){
			Werkproces wp = sender.getWp();
			Criterium cri = new Criterium(sender.getTxtPrestatieIndicator().getText(), sender.getComponentsCriterium());
			wp.addCompetentie(sender.getActiveCompetentie(), cri);
			sender.dispose();
		}else if(e.getActionCommand().equals("addComp")){
			ArrayList<String> comps = sender.getComponentsCriterium();
			AddObjectOptionPane ap = new AddObjectOptionPane("Component");
			if(ap.getResults().size() > 0){
				comps.add(ap.getResults().get("title"));
			}
			sender.getTblComponents().setModel(new ComponentenTableModel(comps));
		}else if(e.getActionCommand().equals("deleteComp")){
			ArrayList<String> comps = sender.getComponentsCriterium();
			comps.remove(sender.getActiveComponent());
			sender.getTblComponents().setModel(new ComponentenTableModel(comps));
		}
	}

	@Override
	public void mouseClicked(MouseEvent e) {
		if(sender.getActivePanel().equals("tablePanel")){
			int row = sender.getTblCompetenties().getSelectedRow();
			Competentie com = CompetentieBestand.getInstance().getCompetenties().get(row);
			sender.setActiveCompetentie(com);
			
			if(e.getClickCount() == 2){
				if(sender.getActiveCompetentie() != null){
					sender.initForm();
				}
			}
		}else if(sender.getActivePanel().equals("formPanel")){
			int row = sender.getTblComponents().getSelectedRow();
			String com = sender.getComponentsCriterium().get(row);
			sender.setActiveComponent(com);
		}

	}

	@Override
	public void mouseEntered(MouseEvent arg0) {}
	@Override
	public void mouseExited(MouseEvent arg0) {}
	@Override
	public void mousePressed(MouseEvent arg0) {}
	@Override
	public void mouseReleased(MouseEvent arg0) {}
}
